﻿using Devices.IDEXX;
using DevicesView.Logic;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DevicesView
{
    /// <summary>
    /// UCRelustDisplay.xaml 的交互逻辑
    /// </summary>
    public partial class UCRelustDisplay : UserControl
    {
        public UCRelustDisplay()
        {
            InitializeComponent();
        }
        private Devices.Result result;
        public Devices.Result Result {
            get => result;

            set {
                result = value;
                if (result == null)
                {
                    this.DataGridItems.Resources = null;
                    textRe.Text = "";
                    return;
                }
                if(result.ResultDatas==null && result.ResultDatas.Count>0)
                    this.DataGridItems.ItemsSource = null;
                if (result.Remarks != null && result.Remarks.Count > 0)
                    textRe.Text = result.Remarks[0];

                var items = result.ResultDatas.Select(o=>new TestResultItem { Unit=o.Unit, Max=o.Max, Min=o.Min, Remarks=o.Remarks, 
                    Display=o.Display, Type=o.Type, Value=o.Value}).ToList();
                DataGridItems.ItemsSource = items;
                
            }
        }

        public class TestResultItem : Devices.ResultItem,INotifyPropertyChanged
        {
            private object values;
            public object Values
            {
                get
                {
                    return values;
                }
                set {
                    double[] mvm = new double[3];
                    mvm[0] = Min==null?0:Min.GetNum();
                    mvm[1] = Value == null ? 0 : Value.ToString().GetNum();
                    mvm[2] = Max == null ? 0 : Max.GetNum();
                    values = mvm;
                    if (this.PropertyChanged != null)
                        this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Values"));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
}
